package com.chb.prototype.deep;

import java.io.*;

/**
 * @author chihb
 * @create-time 2024/10/23 15:16
 * @description
 */
public class Test {
    public static void main(String[] args) {
        Undergraduate undergraduate = new Undergraduate();
        undergraduate.setLocal("泉州");
        Student student = new Student();
        student.setName("chihb");
        student.setAge(18);
        student.setScore(100);
        undergraduate.setStu(student);
        Undergraduate undergraduate1 = (Undergraduate) copy(undergraduate);
        undergraduate1.setLocal("厦门");
        undergraduate1.getStu().setName("libai");
        System.out.println(undergraduate);
        System.out.println(undergraduate1);
        System.out.println(undergraduate1==undergraduate);
    }

    static Object copy(Object obj){
        try(
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\20466\\Desktop\\a.txt"));
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\Users\\20466\\Desktop\\a.txt"));
                ){
            oos.writeObject(obj);
            Object o = ois.readObject();
            return o;
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
